package threadPoll;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

public class NewCachedThreadPoolDemo {

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        ThreadPoolExecutor executor= (ThreadPoolExecutor) executorService;
        System.out.println("初始线程数量："+executor.getActiveCount());//初始线程数量：0
        executorService.execute(()->{
            System.out.println("===========================================");
        });
        System.out.println("执行一次execute时，当前的线程数："+executor.getActiveCount());//执行一次execute时，当前的线程数：1

        IntStream.range(1, 100).boxed().forEach(i->{
            executorService.execute(()->{
                try {
                    TimeUnit.SECONDS.sleep(1);
                    System.out.println("当前线程："+Thread.currentThread().getName()+"：i="+i+"【当前线程池中的线程数："+executor.getActiveCount()+"】");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            });
        });

        TimeUnit.SECONDS.sleep(10);
        System.out.println(((ThreadPoolExecutor) executorService).getActiveCount());
    }
}
